package com.untrance.shop.core.service.bean;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import com.google.appengine.api.datastore.KeyFactory;
import com.untrance.shop.common.vo.UserDetailsVO;
import com.untrance.shop.core.dao.UserDao;
import com.untrance.shop.core.service.UserService;
import com.untrance.shop.model.User;

/**
 * @author eugene zadyra
 */
@Service("userService")
public class UserServiceBean implements UserService {
	@Autowired
	private UserDao userDao;

	@Override
	public User addUser(User user) {
		return userDao.add(user);
	}

	@Override
	public User findUserByUsername(String username) {
		return userDao.findUserByUsername(username);
	}

	@Override
	public User getLoggedInUser() {
		Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
		UserDetailsVO currentUser = (UserDetailsVO)authentication.getPrincipal();
		return userDao.find(KeyFactory.stringToKey(currentUser.getKey()));
	}
}
